package com.clouddot.baipai.util.viewUtil;


import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.widget.TextView;


import com.clouddot.baipai.R;
import com.clouddot.baipai.infrastructure.environment.Env;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by wxy on 2018/4/10.
 */

public class getTimeAndWeatherUtil {

    public static class weekday{
        public int index ;
        public String weekName;
        public weekday(int index,String weekName){
            this.index = index;
            this.weekName = weekName;
        }
    }
    static List<Map<String, String>> mapList = new ArrayList<>();

    public static String getTimeAndData() {
        DateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        Calendar c = Calendar.getInstance();
        final String format = formatter.format(c.getTime()) + " " + getWeekDay();
        return format;
    }


    public static String getData() {
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        final String format = formatter.format(c.getTime());
        return format;
    }

    public static List<weekday> getWeekData(Date date,String endDate) {
        List<weekday> stringList = new ArrayList<>();
        weekday weekday1 = new weekday( 0, "请选择日期");
        stringList.add(weekday1);
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        for(int i = 1; i < 8 ;i ++) {
            String currentDate = formatter.format(c.getTime());
            weekday weekday = new weekday( i, currentDate);
            c.add(Calendar.DAY_OF_MONTH, 1);
            if(currentDate.equals(endDate)) {
                stringList.add(weekday);
                break;
            }
            stringList.add(weekday);
        }
        return stringList;
    }

    public static String getLastData() {
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, -1);
        final String format = formatter.format(c.getTime());
        return format;
    }

    private static String getWeekDay() {
        String Week = "星期";
        Calendar c = Calendar.getInstance();
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            Env.getAppContext().weekday = 7;
            Week += "天";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 2) {
            Env.getAppContext().weekday = 1;
            Week += "一";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 3) {
            Env.getAppContext().weekday = 2;
            Week += "二";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 4) {
            Env.getAppContext().weekday = 3;
            Week += "三";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 5) {
            Env.getAppContext().weekday = 4;
            Week += "四";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 6) {
            Env.getAppContext().weekday = 5;
            Week += "五";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 7) {
            Env.getAppContext().weekday = 6;
            Week += "六";
        }
        return Week;
    }


    public static List<Map<String, String>> getWeatherInfo(String s) throws DocumentException {
        mapList = new ArrayList<>();
        Document document = DocumentHelper.parseText(s);
        Element root = document.getRootElement();
        List<Element> elements = root.elements();
        for (Iterator<Element> it = elements.iterator(); it.hasNext(); ) {
            analysWeather(it);
        }
        return mapList;
    }

    private static void analysWeather(Iterator<Element> it) {
        Element element = it.next();
        List<Element> list = element.elements();
        if (list.size() > 0) {
            for (Iterator<Element> t = list.iterator(); t.hasNext(); ) {
                analysWeather(t);
            }
        }
        Map<String, String> map = new HashMap<>();
        map.put(element.getName(), element.getTextTrim());
        mapList.add(map);
    }

    public static boolean isServiceRunning(final Activity activity, final String className) {
        ActivityManager activityManager = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> info = activityManager.getRunningServices(Integer.MAX_VALUE);
        if (info == null || info.size() == 0) return false;
        for (ActivityManager.RunningServiceInfo aInfo : info) {
            if (className.equals(aInfo.service.getClassName())) return true;
        }
        return false;
    }

    public static void selectTime(FragmentManager manager, final TextView textView) {
        long tenYears = 10L * 365 * 1000 * 60 * 60 * 24L;

        TimePickerDialog mDialogYearMonthDay = new TimePickerDialog.Builder()
                .setType(Type.YEAR_MONTH_DAY)
                .setCallBack(new OnDateSetListener() {
                    @Override
                    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
                        String text = getDateToString(millseconds);
                        textView.setText(text);
                    }
                })
                .setCancelStringId("取消")
                .setSureStringId("确定")
                .setTitleStringId("请选择时间")
                .setYearText("年")
                .setMonthText("月")
                .setDayText("日")
                .setCyclic(false)
                .setMinMillseconds(System.currentTimeMillis())
                .setMaxMillseconds(System.currentTimeMillis() + tenYears)
                .setCurrentMillseconds(System.currentTimeMillis())
                .setThemeColor(Env.getContent().getResources().getColor(R.color.class_left))
                .setWheelItemTextNormalColor(Env.getContent().getResources().getColor(R.color.gray))
                .setWheelItemTextSelectorColor(Env.getContent().getResources().getColor(R.color.red))
                .setWheelItemTextSize(22)
                .build();

        mDialogYearMonthDay.show(manager, "date");
    }

    public static String getDateToString(long time) {
        Date d = new Date(time);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sf.format(d);
    }

}
